// pages/search/search.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgPath:app.globalData.imgPath,
      keyword:'',
      historyList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let historyList = wx.getStorageSync('historyList');
    historyList = this.delRe(historyList);
    historyList = historyList.slice(-10);
    this.setData({ historyList: historyList, keyword:''});
    console.log(historyList)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputkeyword:function(e){
    this.setData({keyword:e.detail.value})
  },
  search:function(){
    let keyword=this.data.keyword;
    if(keyword){
      let historyList = this.data.historyList;
      
      historyList.push(keyword);
      wx.setStorage({
        key: 'historyList',
        data: historyList,
      })
      wx.navigateTo({
        url: '/pages/goodsList/goodsList?key='+keyword+'&r=1',
      })
    }else{
      wx.showToast({
        title: '请输入关键字',
        icon:'none'
      })
    }
  },
  delRe:function(arr){
    var temp = []; //一个新的临时数组
    for (var i = 0; i < arr.length; i++) {
      if (temp.indexOf(arr[i]) == -1) {
        temp.push(arr[i]);
      }
    }
    return temp;
  },
  historysearch:function(e){
    let keyword=e.currentTarget.dataset.word;
    wx.navigateTo({
      url: '/pages/goodsList/goodsList?key=' + keyword+'&r=1',
    })
    
  },
  del:function(){
    let self=this;
    wx.showModal({
      title: '确定删除？',
      success:function(res){
        if(res.confirm){
          self.setData({historyList:[]});
          let list=[]
          wx.setStorageSync('historyList', list);
         
        }
      }
    })
  }
})